package com.itheima.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.feign.client.MovieClient;
import com.itheima.feign.client.UserClient;
import com.itheima.feign.common.ResultInfo;
import com.itheima.user.common.JwtUtil;
import com.itheima.user.domain.User;
import com.itheima.user.mapper.UserMapper;
import com.itheima.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author fanYuLe
 * @Date 2023/3/25 10:58
 * @Blog blog.fanYuLe.net/996.mba
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    //1.登录成功后返回token给前端
    @Override
    public String login(String username, String password) {
        //1.判断用户名是否存在
        //select* from user where username = ?
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getUsername,username);
        User user = userMapper.selectOne(qw);
        if(user == null){
            return null;
        }
        //2.判断密码是否正确
        if(!StrUtil.equals(password,user.getPassword())){
            return null;
        }
        //3.制作token返回token
        Map<String, Object> map = BeanUtil.beanToMap(user);
        String token = JwtUtil.createToken(map);
        return token;
    }
}

